package cc.comac.saic.hkhy.jsrj.gj.inttools.gitlab;

import java.util.List;

import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.GroupApi;
import org.gitlab4j.api.ProjectApi;
import org.gitlab4j.api.RepositoryFileApi;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.RepositoryFile;
import org.gitlab4j.api.models.Visibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Nemo
 * @ClassName: GitlabClient
 * @Date: 2025/7/2 20:31
 * @Description: 介是嘛？
 **/
@Service
public class GitlabClient {
    @Autowired
    private GitLabApi gitLabApi;

    public Project initProject(String projKey) throws GitLabApiException {
        GroupApi groupApi = gitLabApi.getGroupApi();


        Group group = new Group();
        group.setName(projKey);
        Group group1 = groupApi.addGroup(group);

        String path = group1.getPath();


        // 创建新项目
        Project project = new Project()
            .withName(projKey)
            .withVisibility(Visibility.PUBLIC);


        ProjectApi projectApi = gitLabApi.getProjectApi();
        List<Project> projects = projectApi.getProjects();


        return projectApi.createProject(project);
    }


    public void putProject(String projKey) throws GitLabApiException {
        ProjectApi projectApi = gitLabApi.getProjectApi();
        RepositoryFileApi repositoryFileApi = gitLabApi.getRepositoryFileApi();

        List<Project> projects = projectApi.getProjects();

        // 获取文件内容
        // RepositoryFile file = repositoryFileApi.getFile(projectId, "path/to/file", "branch-name");

        // 创建或更新文件
        RepositoryFile file = new RepositoryFile();
        file.setFilePath("path/to/file1.txt");
        file.setContent("file content");

        repositoryFileApi.createFile("projectId", file, "master", "Add new file");

        // 删除文件
        // repositoryFileApi.deleteFile(projectId, "path/to/file", "master", "Remove file");
    }


}
